package com.csis.framework.domain;

import org.apache.commons.lang3.StringUtils;

public class Page {
	// 每页默认显示12条
	private int count = 10;
	// 当前第几页
	private int curPage = 1;
	// 一共多少条
	private int maxCount = 0;
	private int maxPage = 0;
	private String orderBy = null;
	private String sortBy = null;

	public void prepare() {
		if (count <= 0)
			this.maxPage = 1;

		if (maxCount % count == 0) {
			maxPage = maxCount / count;
		} else {
			maxPage = maxCount / count + 1;
		}

		if (curPage > getMaxPage())
			curPage = maxPage;
		if (curPage < 1)
			curPage = 1;
	}

	/**
	 * prepared split page SQL<BR>
	 * note:need to treat different DB Schema, here is support MySQL 
	 * @param sql
	 * @return
	 */
	public String perPageSql(String sql) {
		prepare();
		if (!StringUtils.isBlank(orderBy))
			sql += " ORDER BY " + orderBy + " " + sortBy;
		if (count > 0)
			sql += " LIMIT " + ((curPage - 1) * count) + ", " + count;
		return sql;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getMaxCount() {
		return maxCount;
	}

	public void setMaxCount(int maxCount) {
		this.maxCount = maxCount;
	}

	public int getMaxPage() {
		return this.maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getSortBy() {
		return sortBy;
	}

	public void setSortBy(String sortBy) {
		this.sortBy = sortBy;
	}

}
